import toolMgr from '@/biz/base/tool.js'
import ruleOpt from '@/biz/option/rule.js'

let _this = null

let companys = []

toolMgr.organization(2).then((res) => {
  if (res.length) {
    res.forEach((item) => {
      if (item.children) {
        item.children.unshift({
          label: item.label,
          orgName: item.orgName,
          title: item.title,
          orgId: item.orgId,
          value: item.value,
          id: item.id
        })
        item.id = item.id + 'a'
      }
    })
  }
  companys = res
  treeItem.tree.options = companys
})

let treeItem = {
  title: '展示组织',
  key: 'orgIdList',
  col: 12,
  tree: {
    options: companys,
    saveParent: false,
    saveAll: false,
    maxTagCount: 3
  },
  rules: [ruleOpt.required('请选择展示组织', 'array')],
  data: {
    from: (info) => {
      return info.orgIdList
    }
  }
}

function options(vm) {
  _this = vm
  let pcBanner = {
    title: 'PC Banner',
    key: 'pcAnnex',
    placeholder: '1.仅可上传1张图片，大小不超过10M；2.图片建议宽度1680像素,高度60像素',
    visible: _this.editInfo.type == 'NORMAL',
    uploadImg: {
      title: '图片',
      api: toolMgr.unloadFile(),
      files: [],
      multiple: false,
      hasCover: false,
      before: (file) => {
        return _this.checkImageWH(file, '1680', '60')
      },
      preview: (file) => {
        console.log(file)
        toolMgr.downloadFile(file.url, file.name)
      },
      show: false,
      hideDel: false,
      maxlength: 1,
      maxSize: 10240,
      format: ['jpg', 'jpeg', 'png'],
      formatErr: () => {
        _this.$Message.error('只支持上传jpg、jpeg、png图片！')
      }
    },
    data: {
      to: (v) => {
        let pcAnnex = v.map((item) => ({
          name: item.name,
          url: item.response ? item.response.data.imageURL : item.url
        }))

        _this.editInfo.pcImgUrl = pcAnnex.length ? pcAnnex[0].url : ''

        return {
          pcAnnex
        }
      }
    }
  }

  let relateContent = _this.acName
    ? {
        group: [
          {
            placeholder: _this.acName,
            text: {},
            col: 2
          },
          {
            button: {
              type: 'text',
              title: '查看关联',
              style: {
                color: '#4A6DDB'
              },
              click: () => {
                _this.searchAbout()
              }
            },
            col: 2
          },
          {
            button: {
              type: 'text',
              title: '变更关联',
              style: {
                color: '#4A6DDB'
              },
              click: () => {
                _this.openAc()
              }
            },
            col: 2
          }
        ]
      }
    : {
        button: {
          type: 'text',
          title: '添加关联',
          style: {
            color: '#4A6DDB'
          },
          click: () => {
            _this.openAc()
          }
        }
      }

  let special = [
    {
      title: '关联业务',
      key: 'businessType',
      visible: _this.editInfo.type == 'SPECIAL',
      col: 8,
      select: {
        options: _this.$dt.dictInfo().advertisementBusinessType
      }
    },
    {
      title: '关联内容',
      key: 'aboutContent',
      visible: _this.editInfo.type == 'SPECIAL',
      ...relateContent
    },
    {
      title: '跳转方式',
      key: 'skipMode',
      visible: _this.editInfo.type == 'SPECIAL',
      radio: {
        options: [
          { key: 'DIRECT', value: '直接跳转' },
          { key: 'DETAIL_PAGE', value: '详情页跳转' }
        ]
      },
      change: (val) => {
        if (val.value == 'DIRECT') {
          _this.direct = 1
        } else {
          _this.direct = 2
        }
        _this.updateForm()
      },
      rules: [ruleOpt.required('请选择跳转方式', 'string')]
    },
    {
      title: '按钮文字',
      key: 'buttonText',
      col: 8,
      visible: _this.direct == 2 && _this.isSpecial,
      placeholder: '请输入按钮文字',
      input: {
        type: 'text',
        maxlength: 10
      },
      rules: [ruleOpt.required('请输入按钮文字')]
    }
  ]
  return [
    {
      title: '广告名称',
      key: 'name',
      placeholder: '请输入广告名称',
      input: {
        maxlength: 20
      },
      col: 12,
      tips: '广告名称仅作用于后台辨识，不会在前端展示',
      rules: [ruleOpt.required('广告名称不能为空')]
    },
    {
      title: '广告类型',
      key: 'type',
      radio: {
        options: [
          {
            key: 'NORMAL',
            value: '普通广告'
          },
          {
            key: 'SPECIAL',
            value: '特殊广告'
          }
        ]
      },
      change: (val) => {
        _this.direct = 3
        _this.editInfo.skipMode = null
        if (val.value == 1) {
          pcBanner.visible = true
          _this.isSpecial = false
          special[0].visible = false
          special[1].visible = false
          special[2].visible = false
        } else {
          pcBanner.visible = false
          _this.isSpecial = true
          special[0].visible = true
          special[1].visible = true
          special[2].visible = true
        }
        _this.updateForm()
      }
    },
    ...special,
    {
      title: 'APP Banner',
      key: 'appAnnex',
      placeholder: '1.仅可上传1张图片，大小不超过10M；2.图片建议宽度686像素,高度168像素',
      uploadImg: {
        title: '图片',
        api: toolMgr.unloadFile(),
        files: [],
        hasCover: false,
        before: (file) => {
          return _this.checkImageWH(file, '686', '168')
        },
        preview: (file) => {
          console.log(file)
          toolMgr.downloadFile(file.url, file.name)
        },
        show: false,
        hideDel: false,
        maxlength: 1,
        maxSize: 10240,
        format: ['jpg', 'jpeg', 'png'],
        formatErr: () => {
          _this.$Message.error('只支持上传jpg、jpeg、png图片！')
        }
      },
      data: {
        to: (v) => {
          let appAnnex = v.map((item) => ({
            name: item.name,
            url: item.response ? item.response.data.imageURL : item.url
          }))

          _this.editInfo.appImgUrl = appAnnex.length ? appAnnex[0].url : ''

          return {
            appAnnex
          }
        }
      }
      // rules: [ruleOpt.required('APP Banner不能为空','array')]
    },
    pcBanner,
    {
      title: '展示端口',
      key: 'portList',
      checkbox: {
        options: _this.$dt.dictInfo().advertisementPort
      },
      rules: [ruleOpt.required('请选择展示端口', 'array')]
    },
    treeItem,
    {
      title: '广告内容',
      key: 'content',
      placeholder: '请输入正文',
      visible: _this.direct != 1,
      editor: {},
      rules: [ruleOpt.required('广告内容不能为空')]
    }
  ]
}

export default {
  options
}
